<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Curriculum extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $lost = db( "eb_special_subject" )->select();
        $where = [];
        $where['type']=3;
        $list = db( "eb_special" )->where( $where )->paginate( 5 );
        $data = $list->items();
        foreach( $data as $k=>$v ){
            $data[$k]["subject"] = db( "eb_special_subject" )->where( ["id"=>$v['subject_id']] )->value( "name" );
            $data[$k]["sum"] = db( "eb_special_source" )->where( ["special_id"=>$v["id"]] )->count();
            $data[$k]["recommend"] = db( "eb_recommend_relation" )->where( ["link_id"=>$v["id"]] )->column( "recommend_id" );
        }
        foreach( $data as $kk=>$vv ){
            foreach( $vv['recommend'] as $rk=>$rv ){
                $data[$kk]["recommend_name"][]= db( "eb_recommend" )->where( ["id"=>$rv] )->value( "title" );
            }
        }
        //dump($data);
        $this->assign( "list",$list );
        $this->assign( "data",$data );
        $this->assign( "lost",$lost );
        return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
        $lost = db( "eb_special_subject" )->select();
        $this->assign( "list",$lost );
        return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $inputdata = input();
        $data = [];
        $data['title'] = $inputdata['title'];
        $data['subject_id'] = $inputdata['subject_id'];
        $data['admin_id'] = $this->uid;
        $data['type'] = 3;
        $data['abstract'] = $inputdata['abstract'];
        $data['phrase'] = $inputdata['phrase'];
        $data['image'] = $inputdata['image'];
        $data['label'] = $inputdata['label'];
        $data['banner'] = $inputdata['banner'];
        $data['poster_image'] = $inputdata['poster_image'];
        $data['service_code'] = $inputdata['service_code'];
        $data['money'] = $inputdata['money'];
        $data['pink_money'] = $inputdata['pink_money'];
        $data['is_pink'] = $inputdata['is_pink'];
        $data['pink_number'] = $inputdata['pink_number'];
        $data['pink_strar_time'] = strtotime($inputdata['pink_strar_time']);
        $data['pink_end_time'] = strtotime($inputdata['pink_end_time']);
        $data['pink_time'] = $inputdata['pink_time'];
        $data['is_fake_pink'] = $inputdata['is_fake_pink'];
        $data['fake_pink_number'] = $inputdata['fake_pink_number'];
        $data['sort'] = $inputdata['sort'];
        $data['member_money'] = $inputdata['member_money'];
        $data['member_pay_type'] = $inputdata['member_pay_type'];
        $data['link'] = $inputdata['link'];
        $data['content'] = $inputdata['content'];
        $data['pay_type'] = $inputdata['pay_type'];
        $special_id = db( "eb_special" )->insertGetId( $data );
        $arr = explode( ',',$inputdata['matter_id'] );
        $dataall = [];
        foreach( $arr as $k=>$v ){
            if( !empty( $v ) ){
                $sdata = [];
                $sdata['special_id'] = $special_id;
                $sdata['pay_status'] = $inputdata['pay_type'];
                $sdata['source_id'] = $v;
                $dataall[] = $sdata;
            }
        }
        $res = db( "eb_special_source" )->insertAll( $dataall );

        if( $res ){
            $this->success( "成功",url( "Curriculum/index" ) );
        }else{
            $this->error( "失败" );
        }
    }
    public function getsu()
    {
        $name=input('title');

        $where=[];
        $where['type']=3;
        if($name){
            $where['title']=$name;
        }

        $list=db('eb_special_task')->where($where)->paginate(3);
        $data=$list->items();
        $this->assign("data",$data);
        $this->assign("list",$list);
        return $this->fetch();

    }
    public function lod()
    {
        $data=input();
        $id= implode(',',$data['data']);
        $where = [];
        $data['type']=3;
        $where[]= ["id","in",$id];
        $res=db('eb_special_task')->where($where)->select();
        if($res){
            return json(["code"=>0,"data"=>$res]);
        }else{
            return json(["code"=>1,"data"=>'']);
        }
    }
    public  function upload()
    {
        $file = request()->file('file');

        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀

        // 上传到七牛后保存的文件名
        $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = '6IeVuqWlzQ9f3Vz19UVOAbDDxmwjZpZdf8VId5F_';
        $secretKey = '5T3TJ_O0x1rUOGtaw4kktV91nYZqe-NefC1dACk-';
        $auth = new Auth($accessKey, $secretKey);

        // 要上传的空间
        $bucket = '1810php';

        //域名
        $domain = 'qutzb4nvh.hb-bkt.clouddn.com';
        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();

        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

        if ($err !== null) {
            return ["err" => 1, "msg" => $err, "data" => ""];
        } else {

            //返回图片的完整URL
            $imgPath = 'http://' . $domain . '/' . $key;
            //赋值
            return json(['code' => 0, 'data' => $imgPath, 'type' => 1, 'name' => $key]);

        }

    }
    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $id = input( "id" );
        $list = db( "eb_special_subject" )->select();
        $lost = db( "eb_special_source" )->where( ["special_id"=>$id] )->column("source_id");
        $where = [];
        $where[] = ["id","in",$lost];
        $take = db( "eb_special_task" )->where( $where )->select();
        $find = db( "eb_special" )->where( ["id"=>$id] )->find();
        $this->assign( "data",$find );
        $this->assign( "list",$list );
        $this->assign( "lost",$take );
        return $this->fetch();
    }
    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $data=input();
        $data['type']=3;
        $matter=$data['matter_id'];
        unset($data['matter_id']);
        $res=db('eb_special')->where('id',$data['id'])->update($data);
        if($res){
            $special_id=$data['id'];
            $source_id=trim($matter, ",");//函数移除字符串两侧的空白字符或其他预定义字符
            dump($source_id);
            $loe=db('eb_special_source')->where('special_id',$special_id)->update(['source_id'=>$source_id]);
            if($loe){
                return $this->success('编辑成功',url('Curriculum/index'));
            }else{
                return $this->error('编辑特失败');
            }
        }else{
            return $this->error('编辑十分失败');
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $id = input('id');
        $res = db('eb_special')->where('id', $id)->delete();
        if($res){
            $this->success('删除成功','Curriculum/index');
        }else{
            $this->error('删除失败');
        }
    }
}
